package com.chen.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.entity.User;
import com.chen.mapper.UserMapper;
import com.chen.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.vo.UserVO;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author chen
 * @since 2022-12-13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public Page<User> selectUserByPage(Page<User> page, UserVO userVO) {
        //这是多表联查返回的结果，但是这个结果如何分页呢?
        List<User> userList = this.baseMapper.getUserByPage(page, userVO);
        page.setRecords(userList);
        return page;
    }

    @Override
    public List<User> selectUserList(UserVO userVO) {
        //这是多表联查返回的结果，但是这个结果如何分页呢?
        List<User> userList = this.baseMapper.getUserList(userVO);
        return userList;
    }

    @Override
    public void exportUserExcel(HttpServletResponse response, List<User> userList) throws IOException {
        //----- 写入excel文件
        String exportFileName = "TbUserInfo.xlsx";
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.addHeader("Content-Disposition", "attachment;filename=" + exportFileName);
        EasyExcel.write(response.getOutputStream(), User.class)
                .sheet("用户信息")
                //自动列宽
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .doWrite(userList);
    }
}
